package com.zelin.seckill.web.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.zelin.entity.SeckillGoodsEntity;
import com.zelin.entity.SeckillOrderEntity;
import com.zelin.entity.SeckillStatus;
import com.zelin.entity.vo.PayVo;
import com.zelin.pay.service.PayService;
import com.zelin.seckill.service.SecKillService;
import com.zelin.seckill.service.SeckillOrderService;
import com.zelin.utils.MyDate;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * ------------------------------
 * 功能：
 * 作者：WF
 * 微信：hbxfwf1359033912
 * 创建时间：2021/8/19-15:54
 * ------------------------------
 */
@Controller
public class SecKillOrderController {

    @Reference(timeout = 5000)
    private PayService payService;

    @Reference(timeout = 5000)
    private SeckillOrderService seckillOrderService;
    @Reference(timeout = 5000)
    private SecKillService seckillGoodsService;
    /**
     * 功能: 下单
     * 参数:
     * 返回值: java.lang.String
     * 时间: 2021/8/19 15:54
     */
    @RequestMapping("saveOrder")
    public String saveOrder(String secKillId,Model model){

        //0. 得到日期菜单
        Date date = MyDate.getDateMenus().get(0);
        String dateStr = MyDate.getDateStr(date);
        //0. 得到登录用户名
        String name = SecurityContextHolder.getContext().getAuthentication().getName();

        //1. 下单
        boolean b = seckillOrderService.save(secKillId,dateStr,name);

        //2. 放订单号到model中
        model.addAttribute("outTradNo",secKillId);

        //3. 到支付页面
        return "pay";
    }

    /**
     * 功能: 查询订单
     * 参数:
     * 返回值: java.lang.String
     * 时间: 2021/8/20 16:47
     */
    @RequestMapping("query")
    public String query(Model model){
        //0. 得到登录用户名
        String name = SecurityContextHolder.getContext().getAuthentication().getName();
        //1. 得到用户状态对象
        SeckillStatus seckillStatus = seckillOrderService.querySeckillStatus(name);
        //2. 放到model中
        model.addAttribute("seckillStatus",seckillStatus);
        //3. 跳转
        return "pay";

    }

    /**
     * 功能: 秒杀支付
     * 参数:
     * 返回值:
     * 时间: 2021/8/20 16:47
     */
    @RequestMapping("pay")
    @ResponseBody
    public String pay(){
        //0. 得到登录名
        String name = SecurityContextHolder.getContext().getAuthentication().getName();
        //1. 从redis中查询出订单并返回payVo对象
        PayVo payVo = seckillOrderService.getPayVo(name);
        String result = payService.pay(payVo, 2);
        return result;
    }

    /**
     * 功能: 支付成功后的同步回调
     * 参数:
     * 返回值:
     * 时间: 2021/8/20 17:41
     */
    @RequestMapping("orderlist")
    public String orderlist(Model model){

        //0. 得到登录id
        String name = SecurityContextHolder.getContext().getAuthentication().getName();
        //1. 根据登录用户id查询出此用户的订单列表及此订单的秒杀商品列表
        List<SeckillOrderEntity> orderEntities = seckillOrderService.findOrders(name);
        System.out.println("orderEntities = " + orderEntities);
        model.addAttribute("orderList",orderEntities);

        return "seckill-order";
    }
}
